﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace Akiha.Core
{
    public  class SystemSettingClass
    {

        public static string GetMabPath()
        {
            //string appPath = System.Windows.Forms.Application.StartupPath;
            //string dir = appPath.Substring(0, appPath.LastIndexOf("\\"));
            //string mdbPath = dir + "\\System\\SystemSetting.mdb";
            string mdbPath = System.Windows.Forms.Application.StartupPath + "\\SystemSetting.mdb";
            return mdbPath;
        }

        //获取城镇地籍标准库的mdb文件路径
        private static string GetCZDJBZKPath()
        {
            string mdbPath = Application.StartupPath + "\\testCZDJStandard.mdb";
            //Application.
            return mdbPath;
        }


        //获取地籍库中表中的字段信息
        public static Dictionary<string,string> GetAllFields(string tableName)
        {
            //string mdbPath = System.Windows.Forms.Application.StartupPath + "\\SystemSetting.mdb";
            string mdbPath = GetCZDJBZKPath();
            
            try
            {
               
                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 
                odCommand.CommandText = "select 字段代码,字段名称 from " + tableName;
                //建立读取 
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据 
                int size = odrReader.FieldCount;

                Dictionary<string, string> strDic = new Dictionary<string, string>();

                while (odrReader.Read())
                {            
                    string strFieldCode = odrReader[odrReader.GetName(0)].ToString();
                    string strFieldName = odrReader[odrReader.GetName(1)].ToString();
                    strDic.Add(strFieldCode,strFieldName);
                }
                //关闭连接 
                odrReader.Close();
                odcConnection.Close();
                //读取成功后返回list
                return strDic;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }



        //获取地籍库中的标准表名称
        public static Dictionary<string,string> GetAllLyrName()
        {
            //string mdbPath = System.Windows.Forms.Application.StartupPath + "\\SystemSetting.mdb";
            string mdbPath = GetCZDJBZKPath();
            string tableName = "空间要素分层";
            try
            {

                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 
                odCommand.CommandText = "select 属性表名,层要素  from " + tableName;
                //建立读取 
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据 
                int size = odrReader.FieldCount;

                
                Dictionary<string, string> strDic = new Dictionary<string, string>();
                while (odrReader.Read())
                {
                    string strKey = odrReader[odrReader.GetName(0)].ToString();
                    string strValue = odrReader[odrReader.GetName(1)].ToString();
                    strDic.Add(strKey,strValue);
                    
                }
                //关闭连接 
                odrReader.Close();
                odcConnection.Close();
                //读取成功后返回dic
                return strDic;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }





        /// <summary>
        /// 按照表名如何配置信息表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="success"></param>
        /// <returns></returns>
        public static DataTable ReadAllData(string tableName)
        {
            //string mdbPath = System.Windows.Forms.Application.StartupPath + "\\SystemSetting.mdb";
            string mdbPath = GetMabPath();
            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 
                odCommand.CommandText = "select * from " + tableName;
                //建立读取 
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据 
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i++)
                {
                    DataColumn dc;
                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
                while (odrReader.Read())
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i++)
                    {
                        dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接 
                odrReader.Close();
                odcConnection.Close();
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return dt;
            }
        }

        /// <summary>
        /// 按照表名如何配置信息表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="success"></param>
        /// <returns></returns>
        public static DataTable GetTableFromExecuteSql(string sql)
        {
            //string mdbPath = System.Windows.Forms.Application.StartupPath + "\\SystemSetting.mdb";
            string mdbPath = GetMabPath();
            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 
                odCommand.CommandText = sql;
                //建立读取 
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据 
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i++)
                {
                    DataColumn dc;
                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
                while (odrReader.Read())
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i++)
                    {
                        dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接 
                odrReader.Close();
                odcConnection.Close();
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return dt;
            }
        }

        /// <summary>
        /// 从数据库里读取用地类型
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static List<string> GetTdType(string sql)
        {
            List<string> tdTypeList = new List<string>();
            string mdbPath = GetMabPath();
            try
            {
                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 "SELECT 报批用地文件类型.模版位置 FROM 报批用地文件类型 where 报批用地文件类型.模版名称='关于征地程序补偿安置及社保情况的说明'"
                odCommand.CommandText = sql;
                //建立读取 
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据 
                int size = odrReader.FieldCount;
                while (odrReader.Read())
                {
                    for (int i = 0; i < size; i++)
                    {
                        tdTypeList.Add(odrReader[0].ToString());
                    }
                }
                //关闭连接 
                odrReader.Close();
                odcConnection.Close();
                return tdTypeList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return tdTypeList;
            }
        }

        public static void UpdateTableFromExecuteSql(string sql)
        {
            string mdbPath = GetMabPath();
            try
            {
                DataRow dr;
                //1、建立连接 
                string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
                OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
                //2、打开连接 
                odcConnection.Open();
                OleDbCommand cmd = new OleDbCommand(sql, odcConnection);
                cmd.ExecuteNonQuery();
                odcConnection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
                MessageBox.Show(ex.ToString());
            }
        }

        public static Dictionary<string, string> GetSystemFieldCompareFromMdb(string TableName)
        {
            DataTable DT = ReadAllData(TableName);
            Dictionary<string, string> fieldCompare = new Dictionary<string, string>();
            for (int i = 0; i < DT.Rows.Count; i++)
            {
                string key = DT.Rows[i][1].ToString();
                string value = DT.Rows[i][2].ToString();
                fieldCompare.Add(key, value);
            }
            return fieldCompare;
        }

        public static Dictionary<string, string> GetSystemFieldCompareFromOracle(string TableName)
        {
            string sql = string.Format("select t.SORTNO,t.CODE,t.NAME from sys_code_item t where t.type_code='{0}'", TableName);
            DataTable DT = DbHelperOra.Query(sql).Tables[0];
            List<int> sortNo = new List<int>();
            for (int i = 0; i < DT.Rows.Count; i++)
            {
                sortNo.Add(Convert.ToInt32(DT.Rows[i]["SORTNO"].ToString()));
            }
            sortNo.Sort();
            Dictionary<string, string> fieldCompare = new Dictionary<string, string>();
            for (int i = 0; i < sortNo.Count; i++)
            {
                for (int j = 0; j < DT.Rows.Count; j++)
                {
                    if (sortNo[i] != Convert.ToInt32(DT.Rows[j]["SORTNO"].ToString()))
                        continue;
                    string key = DT.Rows[j]["CODE"].ToString();
                    string value = DT.Rows[j]["NAME"].ToString();
                    fieldCompare.Add(key, value);
                }
            }
            return fieldCompare;
        }

        public static List<string> GetGuids(int count)
        {
            List<string> guids = new List<string>();
            for (int i = 0; i < count; i++)
            {
                string guid = Guid.NewGuid().ToString();
                guids.Add(guid);
            }

            return guids;
        }







    }
}
